package cn.gov.pbc.tims.config;

import java.io.File;
import java.io.IOException;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AppConfig {

    private static final String BUNDLE_NAME="config";
    private static final ResourceBundle RESOURCE_BUNDLE=ResourceBundle.getBundle(BUNDLE_NAME);
    private static final ObjectMapper  OBJECTMAPPER=new ObjectMapper();
    private AppConfig(){
    	
    }
    /**
     * 
     * @param key 
     *            配置文件中的key
     * @return
     */
    		
    public static String getString(String key){
    	if(key.equals("PRO"))
    		return "PRO";
    	try{
	    	String messager=RESOURCE_BUNDLE.getString(key);
	    	return messager;
    	}
    	catch (MissingResourceException e)
    	{
    		String messager="不能在配置文件"+BUNDLE_NAME+"中发现参数："+'!'+key+'!';
    		throw new RuntimeException(messager);
    	}
    }
    public static String getResource(String path){
    	  String basePath=new Object(){
    	    	public String getPath(){
    	    		return this.getClass().getResource("/").getPath();
    	    	}
    	    }.getPath().substring(1);
	        return basePath+path;
    	 	  	  
    	
    } 
    public static JsonNode getJson(String jsonFileName)
    {
    	 JsonNode jsonNode;
		try {
			jsonNode = OBJECTMAPPER.readTree(getResource("json/"+jsonFileName+".json"));
		return jsonNode;
		
		} catch (JsonProcessingException e) {
			String messager="解析"+jsonFileName+".json发生错误";
    		throw new RuntimeException(messager);
		} catch (IOException e) {
			String messager="读取"+jsonFileName+".json文件发生错误，无法找到或打不开该文件";
    		throw new RuntimeException(messager);
		}
    }
  
    
    
}
